﻿Imports System.Windows
Imports System.Windows.Controls

Partial Public Class MyBlogItem
    Inherits UserControl


    Public Sub New()
        Me.InitializeComponent()
        'Me.DataContext = New Blog()
        'TB_Date.Text = CType(Me.DataContext, Blog).PostDate.ToString("yyyy-MM-dd")
    End Sub

    Public Async Function LoadSongTitleAsync() As Task
        Try
            Dim keyword = CType(Me.DataContext, Blog).SongKeyword
            Dim id = CType(Me.DataContext, Blog).SongID
            If String.IsNullOrEmpty(keyword) Or String.IsNullOrEmpty(id) Then
                'MessageBox.Show("获取歌曲信息失败：歌曲信息为空")
                Return
            End If
            Dim _client As New HttpClientBase()
            Dim response = Await _client.GetAsync(Of Song)(String.Format("vdm/api/music/song/{0}/{1}", keyword, id))
            If response.Code <> 200 Then
                'MessageBox.Show("获取歌曲信息失败:" + response.Message)
                Return
            End If
            TB_SongTitle.Text = response.Data.Title
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Console.WriteLine(ex.StackTrace)
            MessageBox.Show("系统错误：" & ex.Message)
            NavigationService.Navigate(NavigationData.LOGIN_PAGE_URI)
        End Try
    End Function

    Private Async Function DeletePostAsync() As Task
        Try
            Dim blogId As Integer = CType(Me.DataContext, Blog).Id
            Console.WriteLine("Delete blog with ID: " & blogId)
            'Delete the blog with the given ID from the database
            Dim _client As New HttpClientBase()
            Dim response = Await _client.DeleteAsync(Of String)("vdm/api/blog/deleteBlog/id/" + blogId.ToString())
            If response.Code = 200 Then
                MessageBox.Show("所选漂流瓶已经被删除")
                NavigationService.Navigate(NavigationData.MAIN_LAYOUT_URI)
            Else
                MessageBox.Show("删除漂流瓶失败，请稍后再试")
            End If
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Console.WriteLine(ex.StackTrace)
            MessageBox.Show("系统错误：" & ex.Message)
            NavigationService.Navigate(NavigationData.LOGIN_PAGE_URI)
        End Try
    End Function

    Private Sub BUT_Delete_Click(sender As Object, e As RoutedEventArgs)
        If MessageBox.Show("确定要删除该漂流瓶吗？", MessageBoxButton.OKCancel) = MessageBoxResult.Cancel Then
            Exit Sub
        End If
        Call DeletePostAsync()
    End Sub

End Class
